
package com.plian.system.service.fr.indicatorsvalue.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.system.entity.fr.indicators.GraphicOrgRelation;
import com.plian.system.mapper.fr.indicators.GraphicOrgRelationMapper;
import com.plian.system.service.fr.indicatorsvalue.IGraphicOrgRelationService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author jianglei
 * @since 2019-10-23
 */
@Service
@AllArgsConstructor
public class GraphicOrgRelationServiceImpl extends ServiceImpl<GraphicOrgRelationMapper, GraphicOrgRelation> implements IGraphicOrgRelationService {
    /**
     * logger属性.
     */
    private static final Logger LOGGER  = LoggerFactory.getLogger(GraphicOrgRelationServiceImpl.class);

    @Override
    public List<String> getSubtract(String parentId) {
        List<GraphicOrgRelation> graphicOrgRelations = list(new QueryWrapper<GraphicOrgRelation>()
                .eq("parent_id", parentId).eq("type", 1));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getAdd(String parentId) {
        List<GraphicOrgRelation> graphicOrgRelations = list(new QueryWrapper<GraphicOrgRelation>()
                .eq("parent_id", parentId).eq("type", 3));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getSubtract(String parentId, String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getParentId, parentId).eq(GraphicOrgRelation::getType, 1));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getSubtract(String parentId, int year) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getParentId, parentId)
                .eq(GraphicOrgRelation::getType, 1)
                .apply(" date_format(end_date, '%Y') >= " +year)
                .apply(" date_format(start_date, '%Y') <= " +year));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getAdd(String parentId, String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getParentId, parentId).eq(GraphicOrgRelation::getType, 3)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getAdd(String parentId, int year) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getParentId, parentId).eq(GraphicOrgRelation::getType, 3)
                .apply(" date_format(end_date, '%Y') >= " +year)
                .apply(" date_format(start_date, '%Y') <= " +year));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getAllParentId(String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .ne(GraphicOrgRelation::getType, 2)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getParentId).distinct().collect(Collectors.toList());
    }

    @Override
    public Map<String, List<GraphicOrgRelation>> getMergeMap(String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getType, 2)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }

        return graphicOrgRelations.stream().collect(Collectors.groupingBy(graphicOrgRelation -> graphicOrgRelation.getParentId()
                + "-" + graphicOrgRelation.getParentName()));
    }

    @Override
    public Map<String, List<GraphicOrgRelation>> getMergeMap(int year) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getType, 2)
                .apply(" date_format(end_date, '%Y') >= " +year)
                .apply(" date_format(start_date, '%Y') <= " +year));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }

        return graphicOrgRelations.stream().collect(Collectors.groupingBy(graphicOrgRelation -> graphicOrgRelation.getParentId()
                + "-" + graphicOrgRelation.getParentName()));
    }

    @Override
    public List<String> getMergeChildList(String parentId) {
        List<GraphicOrgRelation> graphicOrgRelations = list(new QueryWrapper<GraphicOrgRelation>()
                .eq("parent_id", parentId).eq("type", 2));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getSubtractChildList(String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getType, 1)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getAddChildList(String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getType, 3)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getChildId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getSubtractParentList(String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getType, 1)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getParentId).distinct().collect(Collectors.toList());
    }

    @Override
    public List<String> getAddParentList(String reportTime) {
        List<GraphicOrgRelation> graphicOrgRelations = list(Wrappers.<GraphicOrgRelation>lambdaQuery()
                .eq(GraphicOrgRelation::getType, 3)
                .apply(" date_format(end_date, '%Y-%m') >= '" + reportTime + "'")
                .apply(" date_format(start_date, '%Y-%m') <= '" + reportTime + "'"));
        if (CollectionUtil.isEmpty(graphicOrgRelations)){
            return null;
        }
        return graphicOrgRelations.stream().map(GraphicOrgRelation::getParentId).distinct().collect(Collectors.toList());
    }

    @Override
    public String getParentNameByParentId(String parentId) {
        List<GraphicOrgRelation> graphicOrgRelations = list(new QueryWrapper<GraphicOrgRelation>().eq("parent_id", parentId));
        if (CollectionUtil.isNotEmpty(graphicOrgRelations)){
            return graphicOrgRelations.get(0).getParentName();
        }
        return null;
    }
}
